<!--
 * @Description: 酒店行程信息管理
 * @Version: 2.0
 * @Autor: 许国辉
 * @Date: 2022-10-26 08:21:01
 * @LastEditors: 许国辉
 * @LastEditTime: 2023-05-18 10:54:47
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="customHeader"> 导出表格 </a-button>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, toRaw } from 'vue';
  import { getHotelJourneyByParams } from '/@/api/travelItinerary/hotelItineraryAPI';
  import { Tabs } from 'ant-design-vue';
  import { BasicTable, useTable } from '/@/components/Table';
  import { columns, searchFormSchema } from './hotelItineraryData';
  import { useDrawer } from '/@/components/Drawer';
  import { jsonToSheetXlsx } from '/@/components/Excel';
  import { JourneyHotelModel } from '/@/api/travelItinerary/model/journey/JourneyHotelModel';
  import dayjs from 'dayjs';
  export default defineComponent({
    components: {
      [Tabs.name]: Tabs,
      [Tabs.TabPane.name]: Tabs.TabPane,
      BasicTable,
    },
    setup() {
      const journeyHotelModel = ref<JourneyHotelModel>();
      const visible = ref<boolean>(false);
      let api = ref(getHotelJourneyByParams);
      let title = ref('普通订单');
      const [register] = useDrawer();
      const [registerTable, { reload, getDataSource, setColumns, getForm }] = useTable({
        api,
        pagination: { pageSize: 10 },
        columns: columns,
        formConfig: {
          schemas: searchFormSchema,
          fieldMapToTime: [['arrivalDate', ['arrivalDateStart', 'arrivalDateEnd'], 'YYYY-MM-DD']],
        },
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        striped: false,
        useSearchForm: true,
        bordered: true,
        showIndexColumn: false,
      });
      const hideModal = () => {
        visible.value = false;
      };
      function send(record: JourneyHotelModel) {
        // console.log(record);
        journeyHotelModel.value = toRaw(record);
        console.log(journeyHotelModel);
        visible.value = true;
        // carItineraryInfo(true, { ...record });
      }

      function handleSuccess() {
        reload();
      }
      async function tabChange() {
        setColumns(columns);
        title.value = '普通订单';
        api.value = getHotelJourneyByParams;
        await getForm().submit;
        reload();
      }
      function customHeader() {
        let header1 = {
          orderId: '订单号',
          createTime: '预定时间',
          names: '入住人',
          geoName: '入住城市',
          hotelName: '酒店名称',
          arrivalDate: '入住日期',
          departureDate: '离店日期',
          roomCount: '房间数量',
          roomName: '房型',
          status: '状态',
          totalPay: '总房价',
        };
        interface JourneyData {
          orderId: string;
          createTime: string;
          names: string;
          geoName: string;
          hotelName: string;
          arrivalDate: string;
          departureDate: string;
          roomCount: string;
          roomName: string;
          status: string;
          totalPay: string;
        }
        let orderArr: JourneyData[] = [];
        for (let k = 0; k < toRaw(getDataSource()).length; k++) {
          let orderdata = ref<JourneyData>({
            orderId: '订单号',
            createTime: '预定时间',
            names: '入住人',
            geoName: '入住城市',
            hotelName: '酒店名称',
            arrivalDate: '入住日期',
            departureDate: '离店日期',
            roomCount: '房间数量',
            roomName: '房型',
            status: '状态',
            totalPay: '总房价',
          });
          orderdata.value!.orderId = toRaw(getDataSource())[k].orderId;
          orderdata.value!.createTime = toRaw(getDataSource())[k].createTime;
          orderdata.value!.createTime =
            toRaw(getDataSource())[k].createTime.substring(0, 4) +
            '-' +
            toRaw(getDataSource())[k].createTime.substring(4, 6) +
            '-' +
            toRaw(getDataSource())[k].createTime.substring(6, 8) +
            ' ' +
            toRaw(getDataSource())[k].createTime.substring(8, 10) +
            ':' +
            toRaw(getDataSource())[k].createTime.substring(10, 12) +
            ':' +
            toRaw(getDataSource())[k].createTime.substring(12, 14);

          orderdata.value!.names = toRaw(getDataSource())[k].names;
          orderdata.value!.geoName = toRaw(getDataSource())[k].geoName;
          orderdata.value!.hotelName = toRaw(getDataSource())[k].hotelName;
          orderdata.value!.arrivalDate = toRaw(getDataSource())[k].arrivalDate;
          orderdata.value!.departureDate = toRaw(getDataSource())[k].departureDate;
          orderdata.value!.roomCount = toRaw(getDataSource())[k].roomCount;
          orderdata.value!.roomName = toRaw(getDataSource())[k].roomName;
          orderdata.value!.status = toRaw(getDataSource())[k].status;
          if (toRaw(getDataSource())[k].status === 'WAIT_CONFIRM') {
            orderdata.value!.status = '等待确认';
          } else if (toRaw(getDataSource())[k].status === 'WAIT_PAY') {
            orderdata.value!.status = '等待支付';
          } else if (toRaw(getDataSource())[k].status === 'CONFIRM') {
            orderdata.value!.status = '已确认';
          } else if (toRaw(getDataSource())[k].status === 'NO_SHOW') {
            orderdata.value!.status = '未入住';
          } else if (toRaw(getDataSource())[k].status === 'CHECK_OUT') {
            orderdata.value!.status = '已离店';
          } else if (toRaw(getDataSource())[k].status === 'CANCEL') {
            orderdata.value!.status = '已取消';
          } else if (toRaw(getDataSource())[k].status === 'CHECK_IN') {
            orderdata.value!.status = '已入住';
          } else if (toRaw(getDataSource())[k].status === 'INVALID') {
            orderdata.value!.status = '无效';
          }
          orderdata.value!.totalPay = toRaw(getDataSource())[k].totalPay;

          orderArr.push(orderdata.value);
        }
        jsonToSheetXlsx({
          data: orderArr,
          header: header1,
          filename: '酒店行程信息' + dayjs(new Date()).format('YYYY- MM - DD') + '.xlsx',
          json2sheetOpts: {
            // 指定顺序
            // header: ['name', 'id'],
          },
        });
      }
      onMounted(() => {
        //employeeData.initEmployeeData();
      });

      return {
        tabChange,
        registerTable,
        handleSuccess,
        register,
        send,
        customHeader,
        hideModal,
        journeyHotelModel,
        visible,
      };
    },
  });
</script>
